package com.fx.controller;

        import java.util.List;
        import java.util.Map;

        import com.alibaba.fastjson.JSONObject;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.ui.Model;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RequestBody;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.ResponseBody;
        import com.fx.entity.CustomerConfig;
        import com.fx.service.CustomerConfigService;
        import com.fx.util.*;

/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:20
 */

@Controller
@RequestMapping("/customerConfig")
public class CustomerConfigController extends BaseController {
    @Autowired
    private CustomerConfigService customerConfigService;
    private static String pageTitle = "客户配置管理";
    private static String pageUrl = "/customerConfig";

    @GetMapping()
    String customerConfig(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "customerConfig/customerConfig";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<CustomerConfig> customerConfigList = customerConfigService.getList(query);
        int total = customerConfigService.getCount(query);
        return renderPage(total, customerConfigList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "customerConfig/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        CustomerConfig customerConfig =customerConfigService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("customerConfig", customerConfig);
        return "customerConfig/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(CustomerConfig customerConfig) {
        String id = customerConfig.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = customerConfigService.save(customerConfig);
        } else {
            result = customerConfigService.updateById(customerConfig);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (customerConfigService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

}
